<?php

/**
 * 会话类
 *
 */

// define('SESSION_LIFE',1440);
// define('SESSION_LIFE',1800); // 半小时
define('SESSION_LIFE', 3600); // 1小时

class session {
	var $session_date = array();
	var $DB;

	function session($DB) {
		$this->DB = $DB;
		session_set_save_handler(
		array(&$this, 'sess_open'),
		array(&$this, 'sess_close'),
		array(&$this, 'sess_read'),
		array(&$this, 'sess_write'),
		array(&$this, 'sess_destroy'),
		array(&$this, 'sess_gc')
		);
	}

	function sess_open($save_path, $session_name) {
		$this->sess_gc(0);
		return true;
	}

	function sess_close() {
		return true;
	}

	function sess_read($sessionid) {
		$this->session_data = $this->DB->query_first("SELECT * FROM sessions WHERE sessionid='$sessionid'");
		if (!empty($this->session_data) AND $this->session_data['expiry'] > time()) {
			return $this->session_data['value'];
		} else {
			return "";
		}
	}

	function sess_write($sessionid, $val) {
		global $userinfo;
		$expiry = time() + SESSION_LIFE;
		$value = addslashes($val);

		$ipaddress = getip();
		$useragent = $_SERVER['HTTP_USER_AGENT'];
		$REQUEST_URI = $_SERVER['REQUEST_URI'];

		$now = time();
		if (empty($userinfo['userid'])) {
			$userinfo['userid'] = 0;
		}

		$query = $this->DB->query("UPDATE sessions SET
                                                 expiry='$expiry',
                                                 userid='" . $userinfo['userid'] . "',
                                                 value='$value',
                                                 ipaddress='" . addslashes($ipaddress) . "',
                                                 useragent='" . addslashes($useragent) . "',
                                                 location='" . addslashes($REQUEST_URI) . "',
                                                 lastactivity='$now'
                                                 WHERE sessionid='$sessionid'
                                                 ");
		if ($this->DB->affected_rows() < 1) {
			$check = $this->DB->query("SELECT * FROM sessions WHERE sessionid='$sessionid'");
			if (!$this->DB->num_rows($check)) {
				$query = $this->DB->query("INSERT INTO sessions (sessionid,expiry,value,userid,ipaddress,useragent,location,lastactivity)
	                                                 VALUES ('$sessionid',$expiry,'$value','{$userinfo['userid']}','" . addslashes($ipaddress) . "','" . addslashes($useragent) . "','" . addslashes($REQUEST_URI) . "','$now')");
			} else {
				$query = $this->DB->query("UPDATE sessions SET
	                                         expiry='$expiry',
	                                         userid='" . $userinfo['userid'] . "',
	                                         value='$value',
	                                         ipaddress='" . addslashes($ipaddress) . "',
	                                         useragent='" . addslashes($useragent) . "',
	                                         location='" . addslashes($REQUEST_URI) . "',
	                                         lastactivity='$now'
	                                         WHERE sessionid='$sessionid'");
			}
		}
		return $query;
	}

	function sess_destroy($sessionid) {
		return $this->DB->query("DELETE FROM sessions WHERE sessionid='$sessionid'");
	}

	function sess_gc($maxlifetime) {
		$query = $this->DB->query("DELETE FROM sessions WHERE expiry<" . time() . "");
		return $this->DB->affected_rows();
	}
}

?>